
//  AudioPlayerView.m
//  Hear
//
//  Created by mac on 2018/3/30.
//  Copyright © 2018年 mac. All rights reserved.
//  播放器界面

#import "HRAudioPlayerView.h"
#import "HRSquareAudioPlayerModel.h"

#define INITVOLUME 0.6

@implementation HRAudioPlayerView

-(id)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
//        [self setBackgroundColor:UIColorFromRGB(0x323232)];
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(2,6);
        self.layer.shadowOpacity = 0.2;

//        _hideBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_hideBtn setImage:[UIImage imageNamed:@"隐藏"] forState:UIControlStateNormal];
//
//        [self addSubview:_hideBtn];
//        [_hideBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self).offset(8 * scaleWidth);
//            make.centerY.equalTo(self).offset(0);
//            make.width.equalTo(@ (29 * scaleWidth));
//            make.height.mas_equalTo(29 * scaleWidth);
//        }];
        
 
        _userHeadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_userHeadBtn];
//        [_userHeadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(_hideBtn.mas_right).offset(8 * scaleWidth);
//            make.centerY.mas_equalTo(_hideBtn.centerY).offset(0);
//            make.width.height.mas_equalTo(42);
//        }];
        [_userHeadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(self.mas_leading).offset(15);
            make.centerY.mas_equalTo(self);
            make.width.height.mas_equalTo(42);
        }];
        _userHeadBtn.layer.cornerRadius = 21;
        _userHeadBtn.layer.masksToBounds = YES;
        
//        _progress = [[UIProgressView alloc] init];
//        [self addSubview:_progress];
//        _progress.tintColor = [UIColor grayColor];
//        [_progress mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(self.userHeadBtn.mas_right).offset(14 * scaleWidth);
//            make.centerY.mas_equalTo(self.userHeadBtn.mas_centerY);
//            make.height.mas_equalTo(2 * scaleHeight);
//            make.right.mas_equalTo(_userHeadBtn.mas_right).offset(179 * scaleWidth);
//        }];
    
        _slider = [[UISlider alloc] init];
        [self addSubview:_slider];
        _slider.tintColor = [UIColor redColor];
        _slider.minimumTrackTintColor = [UIColor colorWithHexString:@"#2EC8FA"];
        UIImage *thumbImage = [self createImageWithColor:[UIColor grayColor]];
        thumbImage = [self circleImage:thumbImage withParam:0];
        [_slider setThumbImage:thumbImage forState:UIControlStateNormal];
        _slider.continuous = YES;
        _slider.value = INITVOLUME;
        
        [_slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.userHeadBtn.mas_right).offset(14 * scaleWidth);
            make.centerY.mas_equalTo(self.userHeadBtn.mas_centerY);
            make.height.mas_equalTo(2 * scaleHeight);
            make.width.mas_equalTo(185 * scaleWidth);
        }];
        
        _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_playBtn];
        [_playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_slider.mas_right).offset(15 * scaleWidth);
            make.centerY.mas_equalTo(_userHeadBtn.centerY).offset(0);
            make.top.mas_equalTo(_userHeadBtn.mas_top).offset(1.5 * scaleWidth);
            make.width.mas_equalTo(37 * scaleWidth);
        }];
        [_playBtn setImage:[UIImage imageNamed:@"播放键_新"] forState:UIControlStateNormal];
//        [_playBtn setImage:[UIImage imageNamed:@"播放键"] forState:UIControlStateSelected];
        
        _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_nextBtn];
        [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(_playBtn.mas_top).offset(4 * scaleWidth);
            make.left.mas_equalTo(_playBtn.mas_right).offset(22 * scaleWidth);
            make.centerY.mas_equalTo(_playBtn.centerY).offset(0 );
            make.right.mas_equalTo(self.mas_right).offset(-8 * scaleWidth);
//            make.width.mas_equalTo(29);
        }];
        [_nextBtn setImage:[UIImage imageNamed:@"下一个_新"] forState:UIControlStateNormal];
        
    }
    return self;
}

#pragma mark 颜色转换为图片

 -(UIImage*)createImageWithColor:(UIColor*)color{
    
    CGRect rect= CGRectMake(0.0f,0.0f,6.0f,6.0f);
     UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context=UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [color CGColor]);
    
    CGContextFillRect(context, rect);
    CGContextAddEllipseInRect(context, rect);
    UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();
    return theImage;
    
}
#pragma mark 图片裁剪成圆形
-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {
    UIGraphicsBeginImageContext(image.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGRect rect = CGRectMake(inset, inset, image.size.width - inset * 2.0f, image.size.height - inset * 2.0f);
    CGContextAddEllipseInRect(context, rect);
    CGContextClip(context);
    
    [image drawInRect:rect];
    CGContextAddEllipseInRect(context, rect);
    CGContextStrokePath(context);
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimg;
}
-(void)setValue:(id)object
{
    HRSquareAudioPlayerModel *obj = (HRSquareAudioPlayerModel *)object;

    NSString *url = obj.UserPictureName;
    if([url containsString:@"http"]) {
        [_userHeadBtn setImageWithURL:[NSURL URLWithString:url] forState:UIControlStateNormal placeholder:[UIImage imageNamed:@""]];
    }
    else
    {
         [_userHeadBtn setImage:[UIImage imageNamed:obj.UserPictureName] forState:UIControlStateNormal];
    }

}

@end
